package com.travelbid.line.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.travelbid.line.pojo.TCategory;

/**
 * A data access object (DAO) providing persistence and search support for
 * TCategory entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.travelbid.line.pojo.TCategory
 * @author MyEclipse Persistence Tools
 */

public class TCategoryDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(TCategoryDAO.class);
	// property constants
	public static final String CATEGORY_NAME = "categoryName";
	public static final String DOC = "doc";
	public static final String PARENT_ID = "parentId";
	public static final String PHONE_NUMBER = "phoneNumber";

	protected void initDao() {
		// do nothing
	}

	public void save(TCategory transientInstance) {
		log.debug("saving TCategory instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TCategory persistentInstance) {
		log.debug("deleting TCategory instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TCategory findById(java.lang.Integer id) {
		log.debug("getting TCategory instance with id: " + id);
		try {
			TCategory instance = (TCategory) getHibernateTemplate().get(
					"com.travelbid.line.pojo.TCategory", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TCategory instance) {
		log.debug("finding TCategory instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TCategory instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TCategory as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCategoryName(Object categoryName) {
		return findByProperty(CATEGORY_NAME, categoryName);
	}

	public List findByDoc(Object doc) {
		return findByProperty(DOC, doc);
	}

	public List findByParentId(Object parentId) {
		return findByProperty(PARENT_ID, parentId);
	}

	public List findByPhoneNumber(Object phoneNumber) {
		return findByProperty(PHONE_NUMBER, phoneNumber);
	}

	public List findAll() {
		log.debug("finding all TCategory instances");
		try {
			String queryString = "from TCategory";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TCategory merge(TCategory detachedInstance) {
		log.debug("merging TCategory instance");
		try {
			TCategory result = (TCategory) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TCategory instance) {
		log.debug("attaching dirty TCategory instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TCategory instance) {
		log.debug("attaching clean TCategory instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TCategoryDAO getFromApplicationContext(ApplicationContext ctx) {
		return (TCategoryDAO) ctx.getBean("TCategoryDAO");
	}
}